//
//  AccountViewModel.swift
//  cxd4iphone
//
//  Created by hexy on 12/17/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
import ReactiveCocoa

class AccountViewModel: NSObject {

    static let shared = AccountViewModel()
    
    override init() {
        
        accountModel = AccountModel.loadAccount()
        registModel =  RegistModel(dict: ["":""])
        bankCardModel = BankCardModel(dict: ["":""])
        needGesture = false
        
    }
    
    var accountModel: AccountModel?
    
    var token: String? {
        
        return accountModel?.token
    }
    var userId: String {
        
        return accountModel?.id ?? ""
    }
    
    var phoneNo: String {
        
        return (accountModel?.mobileNumber)!
    }
    
    var userLogin: Bool {
        
        return token != nil
    }
    
    var bindedCard: Bool {
        
        return bankCardModel.cardNo != nil
    }
    
    var needGesture: Bool
    
    var userAuthen: Bool?
    
    var isUsePhoneNo: Bool?
    
    var isUseUserName: Bool?
    
    
    var registModel: RegistModel
    
    var bankCardModel: BankCardModel
    
    func regist() -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            APIManager.register(self.registModel).subscribeNext({ (result) -> Void in
                
                    xx_JsonPrint(dict: result)
                    subscriber.sendCompleted()
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            return RACDisposable(block: {})
        })
    }
    
    func exit() {
        
        accountModel?.token = nil
        accountModel?.saveAccount()
    }
    func login(loginName: String, passwd: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            APIManager.login(loginName, passwd: passwd).subscribeNext({ (result) -> Void in
                
//                    xx_JsonPrint(dict: result)
                    let dict = result as! [String: AnyObject]
                    let account = AccountModel(dict: dict)
                    account.saveAccount()
                    self.accountModel = account
                    subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            return RACDisposable(block: {})
        })
    }
    
    
    func checkAuthentication() -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.isAuthentication().subscribeNext({ (result) -> Void in
                
               
                let auth = (result as! [String: AnyObject])["auth"] as! String
                
                self.userAuthen = (auth == "1") ? true : false
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })

            return RACDisposable(block: {})
        })
    }
    
    func checkPhoneNo(phoneNo: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            
            APIManager.checkIsUsedPhone(phoneNo).subscribeNext({ (result) -> Void in
                
                
                let isUse = (result as! [String: AnyObject])["isUse"] as! String
                
                self.isUsePhoneNo = (isUse == "true") ? true : false
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }
    
    func checkUserName(userName: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.checkIsUsedUserName(userName).subscribeNext({ (result) -> Void in
                
                
                xx_JsonPrint(dict: result)
                
                let isUse = (result as! [String: AnyObject])["isUse"] as! String
//
                self.isUseUserName = (isUse == "true") ? true : false
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }
    
    
    func getVerifiCode(phoneNo: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.getVerifiCode(phoneNo).subscribeNext({ (result) -> Void in
                
                
                xx_JsonPrint(dict: result)
                
                let code = (result as! [String : AnyObject])["authCode"] as! String
                
                self.registModel.phoneNo = phoneNo
                self.registModel.authCode = code
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }

    
    func changeLoginPwd(oldPwd: String, newPwd: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.updatePwd(oldPwd, newPwd: newPwd).subscribeNext({ (result) -> Void in
                
                
                xx_JsonPrint(dict: result)
                
//                let code = (result as! [String : AnyObject])["authCode"] as! String
//                
//                self.registModel.phoneNo = phoneNo
//                self.registModel.authCode = code
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }
    
    func resetLoginPwd(pwd: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            self.registModel.password = pwd
            APIManager.resetPwd(self.registModel).subscribeNext({ (result) -> Void in
                
                
                xx_JsonPrint(dict: result)
                
                //                let code = (result as! [String : AnyObject])["authCode"] as! String
                //
                //                self.registModel.phoneNo = phoneNo
                //                self.registModel.authCode = code
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }
    
    func queryBankCard() -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            APIManager.queryBankCard().subscribeNext({ (result) -> Void in
                
                
                xx_JsonPrint(dict: result)
                
                //                let code = (result as! [String : AnyObject])["authCode"] as! String
                //
                //                self.registModel.phoneNo = phoneNo
                //                self.registModel.authCode = code
                
                self.bankCardModel = BankCardModel(dict: result as! [String : AnyObject])
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }

    

}
